const gameModel = require('../models/gameModel');

const getGames = async (req, res) => {
    try {
        const games = await gameModel.getGames();
        res.json(games);
    } catch (error) {
        res.status(500).json({ error: '获取游戏列表失败' });
    }
};

const getGameById = async (req, res) => {
    try {
        const game = await gameModel.getGameById(req.params.id);
        if (game) {
            res.json(game);
        } else {
            res.status(404).json({ error: '游戏未找到' });
        }
    } catch (error) {
        res.status(500).json({ error: '获取游戏详情失败' });
    }
};

const addGame = async (req, res) => {
    try {
        const gameId = await gameModel.addGame(req.body);
        res.status(201).json({ id: gameId });
    } catch (error) {
        res.status(500).json({ error: '添加游戏失败' });
    }
};

const updateGame = async (req, res) => {
    try {
        await gameModel.updateGame(req.params.id, req.body);
        res.json({ message: '游戏更新成功' });
    } catch (error) {
        res.status(500).json({ error: '更新游戏失败' });
    }
};

const deleteGame = async (req, res) => {
    try {
        await gameModel.deleteGame(req.params.id);
        res.json({ message: '游戏删除成功' });
    } catch (error) {
        res.status(500).json({ error: '删除游戏失败' });
    }
};

module.exports = {
    getGames,
    getGameById,
    addGame,
    updateGame,
    deleteGame
};